- Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathWavePrint.java
46 lines (38 loc) Β· 934 Bytes
/
WavePrint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
input: 11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
output: 11 12 13 14 24 23 22 21 31 32 33 34 44 43 42 41
*/
packageLecture7;
publicclassWavePrint {
publicstaticvoidmain(String[] args) {
// int[][] arr = new int[3][3];
// arr[0][0] = 11;
int[][] arr = { { 11, 12, 13, 14 }, { 21, 22, 23, 24 },
{ 31, 32, 33, 34 }, { 41, 42, 43, 44 } };
display(arr);
System.out.println();
// wave print logic
for (inti = 0; i < arr.length; i++) {
if (i % 2 == 0) {
for (intj = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
} else {
for (intk = arr[i].length - 1; k >= 0; k--) {
System.out.print(arr[i][k] + " ");
}
}
}
}
publicstaticvoiddisplay(int[][] arr) {
for (inti = 0; i < arr.length; i++) {
for (intj = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}